<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:replace="fragments/custom_base::commonHead"></th:block>
    <th:block th:replace="fragments/custom_link::pear-css"></th:block>
    <link rel="stylesheet" href="/css/other/form.css" />
    <style>
        .push-product-icon-upload .push-product-icon{
            width: 60px;
            height: 60px;
            cursor: pointer;
        }
        .logo-upload .logo{
            width: 100px;
            height: 100px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="formBox">
    <form action="" class="layui-form">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <input type="hidden" name="id" th:value="${webPushProduct.id}">
                    <div class="layui-form-item logo-upload">
                        <label class="layui-form-label">Logo图标</label>
                        <div class="layui-input-block">
                            <div class="logo-preview">
                                <img class="logo" id="logo-preview" th:src="${webPushProduct.logo?:'/default/upload.png'}" alt="上传Logo" />
                            </div>
                            <input id="logo" name="logo" type="hidden" th:value="${webPushProduct.logo?:'/default/upload.png'}"/>
                            <div class="layui-form-mid layui-word-aux">点击即可上传图片,建议格式jpg|png,尺寸:200x200。</div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">应用名称</label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" lay-verify="required" name="name" placeholder="应用名称" th:value="${webPushProduct.name}"
                                   type="text">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">官网地址</label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" lay-verify="required" name="href" placeholder="http://开头的官网地址" th:value="${webPushProduct.href}"
                                   type="text">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">排序</label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" value="0" name="sort" placeholder="正序排序 0开头" th:value="${webPushProduct.sort}"
                                   type="number">
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <div class="formBoxBottom">
            <div class="button-container">
                <button class="layui-btn layui-btn-primary layui-btn-sm" lay-filter="push-product-update" lay-submit="" type="submit">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button class="layui-btn layui-btn-sm" type="reset">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</div>


<th:block th:replace="fragments/custom_link::pear-js"></th:block>
<script th:inline="javascript">
    layui.use(['form', 'jquery','upload'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let MODULE_PATH = "/admin/push_product/";
        let upload = layui.upload;

        upload.render({
            elem: '#logo-preview', //绑定元素
            url: '/upload', //上传接口
            exts: 'gif|png|jpg',
            done: function(res){
                //上传完毕回调
                console.log(res);
                if (res.code === 0) {
                    //清楚已有Logo
                    $('#logo').val(res.data); // 将返回的图片链接保存到隐藏域
                    $('#logo-preview').attr('src', res.data); // 更新Logo预览
                } else {
                    layer.msg(res.msg);
                }
            },
            error: function(res){
                //请求异常回调
                layer.msg(res.msg);
            }
        });

        form.on('submit(push-product-update)', function (data) {
            $.ajax({
                url: MODULE_PATH+'update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.status==200) {
                        layer.msg(result.message, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("PushProductTable", {
                                where: {},
                            });
                        });
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>